using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using System.Threading.Tasks;

namespace MySoftLab.DapFlash.Views
{
    public enum MessageBoxButtons
    {
        OK,
        OKCancel,
        YesNo,
        YesNoCancel
    }

    public enum MessageBoxResults
    {
        OK,
        Cancel,
        Yes,
        No
    }

    public class MessageBox
    {
        public static async Task ShowDialog(string message, string title = "提示")
        {
            var messageButton = new Button
            {
                Content = "确定",
                HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Right,
                Margin = new Avalonia.Thickness(0, 15, 0, 0)
            };

            var window = new Window
            {
                Title = title,
                SizeToContent = SizeToContent.WidthAndHeight,
                CanResize = false,
                MinWidth = 300,
                WindowStartupLocation = WindowStartupLocation.CenterOwner,
                Content = new StackPanel
                {
                    Margin = new Avalonia.Thickness(20),
                    Children =
                    {
                        new TextBlock
                        {
                            Text = message,
                            TextWrapping = Avalonia.Media.TextWrapping.Wrap,
                            MaxWidth = 400
                        },
                        messageButton
                    }
                }
            };

            messageButton.Click += (s, e) =>
            {
                window.Close();
            };

            if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
            {
                await window.ShowDialog(desktop.MainWindow);
            }
        }

        public static async Task<MessageBoxResults> ShowDialogWithResult(string message, string title = "提示", MessageBoxButtons buttons = MessageBoxButtons.OK)
        {
            TaskCompletionSource<MessageBoxResults> tcs = new TaskCompletionSource<MessageBoxResults>();
            
            var buttonsPanel = new StackPanel
            {
                Orientation = Avalonia.Layout.Orientation.Horizontal,
                HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Right,
                Margin = new Avalonia.Thickness(0, 15, 0, 0),
                Spacing = 10
            };

            var messagePanel = new StackPanel
            {
                Margin = new Avalonia.Thickness(20),
                Children =
                {
                    new TextBlock
                    {
                        Text = message,
                        TextWrapping = Avalonia.Media.TextWrapping.Wrap,
                        MaxWidth = 400
                    },
                    buttonsPanel
                }
            };

            var window = new Window
            {
                Title = title,
                SizeToContent = SizeToContent.WidthAndHeight,
                CanResize = false,
                MinWidth = 300,
                WindowStartupLocation = WindowStartupLocation.CenterOwner,
                Content = messagePanel
            };

            // 根据按钮类型添加不同的按钮
            switch (buttons)
            {
                case MessageBoxButtons.OK:
                    var okButton = new Button { Content = "确定", MinWidth = 80 };
                    okButton.Click += (s, e) =>
                    {
                        tcs.SetResult(MessageBoxResults.OK);
                        window.Close();
                    };
                    buttonsPanel.Children.Add(okButton);
                    break;

                case MessageBoxButtons.OKCancel:
                    var okBtn = new Button { Content = "确定", MinWidth = 80 };
                    okBtn.Click += (s, e) =>
                    {
                        tcs.SetResult(MessageBoxResults.OK);
                        window.Close();
                    };
                    
                    var cancelBtn = new Button { Content = "取消", MinWidth = 80 };
                    cancelBtn.Click += (s, e) =>
                    {
                        tcs.SetResult(MessageBoxResults.Cancel);
                        window.Close();
                    };
                    
                    buttonsPanel.Children.Add(okBtn);
                    buttonsPanel.Children.Add(cancelBtn);
                    break;

                case MessageBoxButtons.YesNo:
                    var yesButton = new Button { Content = "是", MinWidth = 80 };
                    yesButton.Click += (s, e) =>
                    {
                        tcs.SetResult(MessageBoxResults.Yes);
                        window.Close();
                    };
                    
                    var noButton = new Button { Content = "否", MinWidth = 80 };
                    noButton.Click += (s, e) =>
                    {
                        tcs.SetResult(MessageBoxResults.No);
                        window.Close();
                    };
                    
                    buttonsPanel.Children.Add(yesButton);
                    buttonsPanel.Children.Add(noButton);
                    break;

                case MessageBoxButtons.YesNoCancel:
                    var yesBtn = new Button { Content = "是", MinWidth = 80 };
                    yesBtn.Click += (s, e) =>
                    {
                        tcs.SetResult(MessageBoxResults.Yes);
                        window.Close();
                    };
                    
                    var noBtn = new Button { Content = "否", MinWidth = 80 };
                    noBtn.Click += (s, e) =>
                    {
                        tcs.SetResult(MessageBoxResults.No);
                        window.Close();
                    };
                    
                    var cancelButton = new Button { Content = "取消", MinWidth = 80 };
                    cancelButton.Click += (s, e) =>
                    {
                        tcs.SetResult(MessageBoxResults.Cancel);
                        window.Close();
                    };
                    
                    buttonsPanel.Children.Add(yesBtn);
                    buttonsPanel.Children.Add(noBtn);
                    buttonsPanel.Children.Add(cancelButton);
                    break;
            }

            window.Closed += (s, e) =>
            {
                if (!tcs.Task.IsCompleted)
                {
                    tcs.SetResult(MessageBoxResults.Cancel);
                }
            };

            if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
            {
                await window.ShowDialog(desktop.MainWindow);
            }

            return await tcs.Task;
        }
    }
} 